name: release
on:
  workflow_dispatch:
    inputs:
      release_type:
        description: 'Release type'
        required: true
        type: choice
        default: 'patch'
        options:
          - major
          - minor
          - patch
defaults:
  run:
    shell: bash --noprofile --norc -exo pipefail {0}
env:
  LINES: 120
  COLUMNS: 120
  OPENLLM_DO_NOT_TRACK: True
  PYTHONUNBUFFERED: '1'
  HATCH_VERBOSE: 2
jobs:
  release:
    if: github.repository_owner == 'bentoml'
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    outputs:
      version: ${{ steps.version.outputs.version }}
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # ratchet:actions/checkout@v4.1.1
        with:
          fetch-depth: 0
          token: ${{ secrets.OPENLLM_PAT }}
      - uses: bentoml/setup-bentoml-action@862aa8fa0e0c3793fcca4bfe7a62717a497417e4 # ratchet:bentoml/setup-bentoml-action@v1
        with:
          bentoml-version: 'main'
          python-version-file: .python-version-default
      - name: Import bot's GPG key for signing commits
        id: import-gpg
        uses: crazy-max/ghaction-import-gpg@01dd5d3ca463c7f10f7f4f7b4f177225ac661ee4 # ratchet:crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}
          git_config_global: true
          git_user_signingkey: true
          git_commit_gpgsign: true
          git_tag_gpgsign: true
      - name: Install jq and curl
        run: sudo apt-get install -y jq curl
      - name: Create a release
        env:
          GIT_AUTHOR_NAME: ${{ steps.import-gpg.outputs.name }}
          GIT_AUTHOR_EMAIL: ${{ steps.import-gpg.outputs.email }}
          GIT_COMMITTER_NAME: ${{ steps.import-gpg.outputs.name }}
          GIT_COMMITTER_EMAIL: ${{ steps.import-gpg.outputs.email }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |-
          pip install ghapi inflection tomlkit orjson
          ./.github/actions/release.sh --release ${{ inputs.release_type }}
      - name: Set project version
        id: version
        run: |-
          version="$(git describe --tags "$(git rev-list --tags --max-count=1)")"
          echo "Release version: $version"
          echo "version=$version" >> $GITHUB_OUTPUT
  build-pypi:
    if: github.repository_owner == 'bentoml'
    needs:
      - release
    name: Compile PyPI distribution for OpenLLM
    uses: bentoml/OpenLLM/.github/workflows/build-pypi.yml@main # ratchet:exclude
    with:
      tags: ${{ needs.release.outputs.version }}
  publish-python:
    needs:
      - build-pypi
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # ratchet:actions/checkout@v4.1.1
        with:
          ref: '${{ needs.release.outputs.version }}'
          token: ${{ secrets.OPENLLM_PAT }}
      # NOTE: Keep this section in sync with build-pypi.yml
      - name: Download Python artifacts
        uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # ratchet:actions/download-artifact@v3
        with:
          name: python-artefacts
          path: dist
      - name: Smoke test compiled artefacts
        run: ls -R dist
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@2f6f737ca5f74c637829c0f5c3acd0e29ea5e8bf # ratchet:pypa/gh-action-pypi-publish@release/v1
        with:
          print-hash: true
  prepare-next-dev-cycle:
    needs:
      - release
      - publish-python
      - binary-distribution
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # ratchet:actions/checkout@v4.1.1
        with:
          fetch-depth: 0
      - name: Setup python
        uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # ratchet:actions/setup-python@v5.0.0
        with:
          python-version-file: .python-version-default
      - name: Install dependencies
        run: pip install hatch towncrier
      - name: Install jq and curl
        run: sudo apt-get install -y jq curl
      - name: Import bot's GPG key for signing commits
        id: import-gpg-key
        uses: crazy-max/ghaction-import-gpg@01dd5d3ca463c7f10f7f4f7b4f177225ac661ee4 # ratchet:crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}
          git_config_global: true
          git_user_signingkey: true
          git_commit_gpgsign: true
          git_tag_gpgsign: true
      - name: Bump version to dev
        env:
          GIT_AUTHOR_NAME: ${{ steps.import-gpg-key.outputs.name }}
          GIT_AUTHOR_EMAIL: ${{ steps.import-gpg-key.outputs.email }}
          GIT_COMMITTER_NAME: ${{ steps.import-gpg-key.outputs.name }}
          GIT_COMMITTER_EMAIL: ${{ steps.import-gpg-key.outputs.email }}
        run: |
          git pull --autostash --no-edit --gpg-sign --ff origin main
          SEMVER="${{ needs.release.outputs.version }}"
          SEMVER="${SEMVER#v}"
          OLD_IFS=$IFS
          IFS='.'
          read -ra VERSION_BITS <<< "$SEMVER"
          IFS=$OLD_IFS
          VNUM1=${VERSION_BITS[0]}
          VNUM2=${VERSION_BITS[1]}
          VNUM3=${VERSION_BITS[2]}
          VNUM3=$((VNUM3+1))
          DEV_VERSION="$VNUM1.$VNUM2.$VNUM3.dev0"
          echo "Bumping version to ${DEV_VERSION}..."
          jq --arg release_version "${DEV_VERSION}" '.version = $release_version' < package.json > package.json.tmp && mv package.json.tmp package.json
          pushd openllm-node &>/dev/null
          jq --arg release_version "${DEV_VERSION}" '.version = $release_version' < package.json > package.json.tmp && mv package.json.tmp package.json
          popd &>/dev/null
          git add package.json openllm-node/package.json && git commit -S -sm "infra: bump to dev version of ${DEV_VERSION} [generated] [skip ci]"
          git push origin HEAD:main
  binary-distribution:
    if: github.repository_owner == 'bentoml'
    needs: build-pypi
    name: Create binary/wheels distribution
    uses: bentoml/OpenLLM/.github/workflows/binary-releases.yml@main # ratchet:exclude
  release-notes:
    if: github.repository_owner == 'bentoml'
    needs:
      - release
      - publish-python
      - binary-distribution
    name: Create release notes and setup for next cycle
    uses: bentoml/OpenLLM/.github/workflows/release-notes.yml@main # ratchet:exclude
    with:
      tags: ${{ needs.release.outputs.version }}
  bump-homebrew-tap:
    needs:
      - release-notes
      - prepare-next-dev-cycle
      - release
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # ratchet:actions/checkout@v4.1.1
        with:
          fetch-depth: 0
      - uses: bentoml/setup-bentoml-action@862aa8fa0e0c3793fcca4bfe7a62717a497417e4 # ratchet:bentoml/setup-bentoml-action@v1
        with:
          bentoml-version: 'main'
          python-version-file: .python-version-default
      - name: Install jq and curl
        run: sudo apt-get install -y jq curl
      - name: Import bot's GPG key for signing commits
        id: import-gpg-key
        uses: crazy-max/ghaction-import-gpg@01dd5d3ca463c7f10f7f4f7b4f177225ac661ee4 # ratchet:crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}
          git_config_global: true
          git_user_signingkey: true
          git_commit_gpgsign: true
          git_tag_gpgsign: true
      - name: Update current formula
        env:
          GIT_AUTHOR_NAME: ${{ steps.import-gpg-key.outputs.name }}
          GIT_AUTHOR_EMAIL: ${{ steps.import-gpg-key.outputs.email }}
          GIT_COMMITTER_NAME: ${{ steps.import-gpg-key.outputs.name }}
          GIT_COMMITTER_EMAIL: ${{ steps.import-gpg-key.outputs.email }}
          GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
        run: |
          git pull --autostash --no-edit --gpg-sign --ff origin main
          SEMVER="${{ needs.release.outputs.version }}"
          SEMVER="${SEMVER#v}"
          pip install fs jinja2 ghapi plumbum
          ./tools/update-brew-tap.py
          git add Formula && git commit -S -sm "infra: bump to homebrew tap release to ${SEMVER} [generated] [skip ci]"
          git push origin HEAD:main
